/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import entity.NutritionZone;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author Administrator
 */
public class NutritionZoneDAO {
    private Connection cn;
    private PreparedStatement pstm;
    private CallableStatement cs;
    
    public NutritionZoneDAO() {
        cn = DB_Util.getConnection();
    }
    
    private ArrayList getNutritionZoneData(ResultSet rs) throws SQLException {
        ArrayList list = new ArrayList();
        while (rs.next()) {
            NutritionZone e = new NutritionZone();
            e.setId(rs.getInt("NutritionZoneId"));
            e.setName(rs.getString("NutritionZoneName"));
            e.setDetail(rs.getString("NutritionZoneDetail"));
            e.setType(rs.getString("NutritionZoneType"));
            list.add(e);
        }
        return list;
    }
    public void addNutritionZone(NutritionZone item) throws SQLException
    {
        cs = cn.prepareCall("{call SP_Add_NutritionZone(?,?,?)}");
        cs.setNString(1, item.getName());
        cs.setNString(2, item.getDetail());
        cs.setNString(3, item.getType());
        cs.execute();
    }
    
    //DISPLAY NUTRITION ZONE:
    public ArrayList getAllNutritionZone() throws SQLException {
        cs = cn.prepareCall("{call SP_Display_NutritionZone}");
        ResultSet rs = cs.executeQuery();
        return getNutritionZoneData(rs);
    }
    public void editNutritionZone(NutritionZone item) throws SQLException{
    cs = cn.prepareCall("{call SP_Edit_NutritionZone(?,?,?,?)}");
    cs.setInt(1,item.getId());
    cs.setNString(2, item.getName());
    cs.setNString(3, item.getDetail());
    cs.setNString(4, item.getType());
    cs.execute();
    }
     public void deleteNutritionZone(int id) throws SQLException{
    cs = cn.prepareCall("{call SP_Delete_NutritionZone(?)}");
    cs.setInt(1,id);
    cs.execute();
    }
}